package cn.xc.basic.controller;

import cn.xc.basic.domain.SmsCodeDto;
import cn.xc.basic.service.IVerifyCodeService;
import cn.xc.basic.util.AjaxResult;
import cn.xc.org.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key) {
        try {
            String imageVerifyCode = verifyCodeService.imageVerifyCode(key);
            return AjaxResult.me().setResultObj(imageVerifyCode);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"獲取驗證碼失敗!");
        }
    }

//    @GetMapping("/email")
//    public String active(){
//        return "激活成功!";
//    }

    /**
     * 獲取手機圖形驗證碼
     * @param smsCodeDto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.smsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系統繁忙，請稍後再試!!");
        }
    }
    /**
     * 微信掃碼獲取手機圖形驗證碼
     * @param smsCodeDto
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.binderSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系統繁忙，請稍後再試!!");
        }
    }

}
